// pages/user/edit.js
import Toast from '@vant/weapp/toast/toast';
import Ajax from "../../../utils/ajax"
import Util from "../../../utils/util"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [  { url: '',  },],
    idFileList1: [  { url: 'https://img.yzcdn.cn/vant/tree.jpg',  }, ],
    idFileList2: [  { url: 'https://img.yzcdn.cn/vant/tree.jpg',  }, ],
    codeFileList1: [  { url: 'https://img.yzcdn.cn/vant/tree.jpg',  },],
    codeFileList2: [  { url: 'https://img.yzcdn.cn/vant/tree.jpg',  },],
    companyFileList: [  { url: 'https://img.yzcdn.cn/vant/tree.jpg',  },],
    name:"",
    address:"",
    company:"",
    companyAddress:"",
    showGender:false,
    showDate:false,
    gender:'男',
    date:'',
    phone:'',
    textarea:'',
    id:'',
    code:'',
    minDate: new Date(1980, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    columns: ['男', '女'],
    user:{}
  },
  
  onLoad: function (options) {
    var user = app.globalData.getUserInfo

    console.log("查看用户数据",user);
    /*
     autonymPath   
 
 companyPath
    */
    if(user){
      user.gender =  user.gender =='0'?'男':'女'
      user.birthday  =  Util.formatDate(user.birthday)
      var tmp1 = [] 
      var tmp2 = [] 
      if(user.autonymPath){
        tmp1 = [{url: user.autonymPath.split(",")[0] }]  
        tmp2 = [{url: user.autonymPath.split(",")[1] }]  
      }
       
      this.setData({  
        user:user,
        idFileList1:tmp1,
        idFileList2:tmp2,
        codeFileList1:[{ url: user.brokerPath}],
        companyFileList:[{ url: user.companyPath}], 
      }); 
    }
  },
  onChange(e) {
    console.log(e.currentTarget.dataset.prop,e.detail.value)   
    this.setData({ user: Object.assign(this.data.user, { [e.currentTarget.dataset.prop]:e.detail.value}) })
  }, 
  onSave(){
    console.log("点击提交");

    var th = this;  
    Ajax.post(Ajax.api.updateUser, Object.assign(this.data.user,{
      birthday:'1995-08-04 11:11:11',
      nikename:'my-nikename',
      status:0,
      autonymPath:this.data.idFileList1[0].url + ',' + this.data.idFileList1[0].url,
      brokerPath:this.data.codeFileList1[0].url,
      companyPath:this.data.companyFileList[0].url,
    })).then((res)=>{
      console.log(res);
      th.setData({   })
    }); 
  },
  onSave1(){   
    console.log( this.data);
    var { name,company,companyAddress,address,gender,date,phone,textarea,id,code,} = this.data
    var { fileList,idFileList1,idFileList2,codeFileList1,codeFileList2,companyFileList} = this.data
    if( !name) return Toast("请输入姓名")
    if(! /^[\u4e00-\u9fa5]{2,6}$/.test(name)) return Toast("请输入正确的姓名")
    if( !date) return Toast("选择出生年月")
    if( !address) return Toast("请输入所在地")
    if( !company) return Toast("请输入公司名称")
    if( !companyAddress) return Toast("请输入公司地址")
    if( !phone) return Toast("请输入手机号") 
    if(! /^1[123456789]\d{9}$/.test(phone)) return Toast("请输入正确的手机号")
    if( !id) return Toast("请输入身份证") 
    if(! /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(id)) return Toast("请输入正确的身份证")
    if( !code) return Toast("请输入执业证号") 
    if(! /[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/.test(code)) return Toast("请输入正确的执业证号")
    if(fileList.length==0)  return Toast("请上传头像") 
    if(idFileList1.length==0)  return Toast("请上传身份证正面") 
    if(idFileList2.length==0)  return Toast("请上传身份证反面") 
    if(codeFileList1.length==0)  return Toast("请上传执业证正面") 
    if(codeFileList2.length==0)  return Toast("请上传执业证反面") 
    if(companyFileList.length==0)  return Toast("请上传公司营业执照") 
    

 },
  deleteImg(event){
    var id = event.currentTarget.dataset.id
    if(id =='user'){ this.setData({ fileList:[] })  } 
    if(id =='id1'){ this.setData({ idFileList1:[] })  } 
    if(id =='id2'){ this.setData({ idFileList2:[] })  } 
    if(id =='code1'){ this.setData({ codeFileList1:[] })  } 
    if(id =='code2'){ this.setData({ codeFileList2:[] })  } 
    if(id =='company'){ this.setData({ companyFileList:[] })  } 
  }, 
  afterRead(event){
    console.log(event);
    var th = this; 
    var id = event.currentTarget.dataset.id // user id1 id2 code1 code2  company
    Ajax.upload(event.detail.file,3,{
      success:function(data){
        console.log("上传后的响应",data);
        var arr = [    { url: data.data.url} ]
        if(id=='user') th.setData({ fileList: arr })
        if(id=='id1') th.setData({ idFileList1: arr })
        if(id=='id2') th.setData({ idFileList2: arr })
        if(id=='code1') th.setData({ codeFileList1: arr })
        if(id=='code2') th.setData({ codeFileList2: arr })
        if(id=='company') th.setData({ companyFileList: arr })
        
      },
    } )
    
  },

  onChangeGender(event) {  this.setData({ gender: event.detail.value ,showGender:false,}) }, 
  onOpenDate() {  this.setData({ showDate:true,}) }, 
  onOpenGender(){ this.setData({ showGender:true,showDate:false}) },
  onClose(){ this.setData({ showGender:false,showDate:false}) },
  onConfirmDate(event){ 
    var date = new Date(event.detail);
    var tmp = this.data.user 
    tmp.birthday =    `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
    this.setData({ showDate:false,user: tmp })
   },
  



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})